﻿using System;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;
using QTMusic.BaseClasses;

namespace QTMusic.UserControls
{
    public partial class FriendListItem : System.Web.UI.UserControl
    {
        private int friendId = 0;

        public int FriendId
        {
            get { return friendId; }
            set { friendId = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string urlFriend = Common.urlWebsites + "Profile.aspx?id=" + friendId.ToString();

            string selectSQL = "SELECT user_name, user_pic FROM [user] WHERE user_id = @friendId";
            SqlConnection conn = new SqlConnection(Common.connString);
            SqlCommand cmd = new SqlCommand(selectSQL, conn);
            cmd.Parameters.AddWithValue("@friendId", friendId);
            SqlDataReader reader;

            try
            {
                using (conn)
                {
                    conn.Open();
                    reader = cmd.ExecuteReader();
                    reader.Read();
                    if (reader["user_pic"] != DBNull.Value)
                    {
                        imgUser.ImageUrl = (string)reader["user_pic"];
                        lnkUserImage.NavigateUrl = urlFriend;
                    }
                    else
                        imgUser.ImageUrl = Common.defaultImage;
                    imgUser.Height = Unit.Pixel(50);
                    imgUser.Width = Unit.Pixel(50);

                    if (reader["user_name"] != DBNull.Value)
                    {
                        lnkUserName.Text = (string)reader["user_name"];
                        lnkUserName.NavigateUrl = urlFriend;
                    }
                    else
                        lnkUserName.Text = "Unknown";
                }
            }
            catch (Exception friendListEx)
            {
                //FriendListItemException newException = new FriendListItemException(friendListEx.Message, friendListEx);
                //throw newException;
                Common.WriteToEventLog(friendListEx, "LoadFriendListItem");
            }
        }
    }

    public class FriendListItemException : ApplicationException
    {
        public FriendListItemException()
            : base()
        {

        }

        public FriendListItemException(string message)
            : base(message)
        {

        }

        public FriendListItemException(string message, Exception inner)
            : base(message, inner)
        {

        }
    }
}